/*
* $Id: ChangeInspectWizard.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import java.util.Vector;

import apiary.Apiary;
import apiary.common.FormHelper;
import apiary.db.FieldsValueStore.FieldsValueRecord;
import apiary.db.InspectStore.InspectRecord;
import apiary.db.managers.ConvertData;
import apiary.db.managers.DBManager;
import apiary.db.managers.InspectManager.InspectManagerRecord;
import apiary.ui.BackScreen;
import apiary.ui.journal.*;
import apiary.ui.journal.ChooseForm.ChooseValue;
import apiary.ui.journal.JournalForm.JournalValue;
import apiary.ui.journal.UserFieldForm.UserField;

/**
 * Wizard of changing inspect of family
 * @author lexus
 */
public class ChangeInspectWizard extends Wizard
{
    /**
     * Temporary value. Contains hive's journalInspects for current inspect.
     */
    private Vector journalInspects;
    /**
     * Constructor
     */
    public ChangeInspectWizard() {
        super(null);
        Init();
    }
    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ChangeInspectWizard(BackScreen parentForm) {
        super(null);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Saves result of wizard
     */
    public void save() {
        int idJournal = ((ChooseValue) getData(new Integer(2))).id;
        JournalValue value = (JournalValue) getData(new Integer(3));
        Vector inspects = (Vector) getData(new Integer(4));
        DBManager.getInstance().changeInspect(idJournal, value, inspects);
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        WizardScreen screen = (WizardScreen) nextForm;
        if(screen.getIdForm().intValue() == 2) {
            ChooseValue value = (ChooseValue) getData(new Integer(1));
            FormHelper.fillInspectList((ChooseForm) nextForm, value.id);
        } else if(screen.getIdForm().intValue() == 3) {
            setJournalData((JournalForm)nextForm);
        } else if(screen.getIdForm().intValue() == 4) {
            setHousingData((InspectHForm) nextForm);
        }
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        return true;
    }

    /**
     * Form is filled hives
     * @param inspectHForm form
     */
    private void setHousingData(InspectHForm inspectHForm) {
        Vector inspects = (Vector) inspectHForm.GetDataForm();
        for(int i = 0; i < journalInspects.size(); i++) {
            InspectRecord record = (InspectRecord) journalInspects.elementAt(i);
            InspectValue value = new InspectValue();
            ConvertData.InspectRecordToValue(record, value);
            inspects.addElement(value);
        }
        inspectHForm.setInspects(inspects);
    }

    /**
     * Form is filled common inspect
     * @param journalForm form
     */
    private void setJournalData(JournalForm journalForm) {
        ChooseValue value = (ChooseValue) getData(new Integer(2));
        InspectManagerRecord record = DBManager.getInstance().getInspect(value.id);
        journalInspects = record.inspects;
        JournalValue journal = journalForm.new JournalValue();
        ConvertData.JournalRecordToValue(record.journal, journal);
        Vector fieldsValue = DBManager.getInstance().getFieldsValue(record.journal.idRecord);
        Vector userFields = new Vector();
        for(int i = 0; i < fieldsValue.size(); i++) {
            FieldsValueRecord fieldValue = (FieldsValueRecord) fieldsValue.elementAt(i);
            UserField userField = journalForm.new UserField();
            ConvertData.FieldsValueToUserField(fieldValue, userField);
            userFields.addElement(userField);
        }
        journal.userFields = userFields;
        journalForm.setJournalValue(journal);
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Apiary apiary = Apiary.getInstance();
        Integer idChoose = new Integer(1);
        firstForm = new ChooseForm(apiary.getLocaleString("ChooseFamily"), idChoose, this, true);
        FormHelper.fillFamilyList((ChooseForm) firstForm);
        Integer idChooseInspect = new Integer(2);
        ChooseForm chooseForm = new ChooseForm(apiary.getLocaleString("ChooseInspect"), idChooseInspect, this, true);
        chooseForm.setBackScreen(firstForm);
        formsNext.put(idChoose, chooseForm);
        Integer idJournal = new Integer(3);
        JournalForm journal = new JournalForm(apiary.getLocaleString("ChangeInspect"), this, idJournal, true);
        journal.setBackScreen(chooseForm);
        formsNext.put(idChooseInspect, journal);
        Integer idInspectH = new Integer(4);
        InspectHForm inspectH = new InspectHForm(apiary.getLocaleString("InspectHousing"), this, idInspectH, false);
        inspectH.setBackScreen(journal);
        formsNext.put(idJournal, inspectH);
    }
}
